package com.ysd.dao.impl;

import java.util.ArrayList;
import com.ysd.entity.Gongsixinxi;
import com.ysd.util.DBHelper;
import com.ysd.util.DataResult;

public class GongsixinxiDaoimpl {
	public int add(Gongsixinxi cpb) {
		String sql=String.format("insert gongsixinxi(rexian,dianhua,youxiang,bizhi) "
				+ "value('%s','%s','%s','%s')"
				,cpb.getRexian()
				,cpb.getDianhua()
				,cpb.getYouxiang()
				,cpb.getBizhi());
		try {
			int  i=DBHelper.execNonQuery(sql);
			return i;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return -1;
	}
	public int delete(int id) {
		String sql="delete from gongsixinxi where id="+id;
		try {
			int  i=DBHelper.execNonQuery(sql);
			return i;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return -1;
	}
	public int update(Gongsixinxi cpb) {
		String sql=String.format("update gongsixinxi set "
				+ "rexian='%s',"
				+ "dianhua='%s',"
				+ "youxiang='%s',"
				+ "bizhi='%s' where "
				+ "id=%d"
				,cpb.getRexian()
				,cpb.getDianhua()
				,cpb.getYouxiang()
				,cpb.getBizhi()
				,cpb.getId());
		try {
			int  i=DBHelper.execNonQuery(sql);
			return i;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return -1;
	}
	public ArrayList<Gongsixinxi> selectAll(){
		ArrayList<Gongsixinxi> list=new ArrayList<Gongsixinxi>();
		String sql="select*from gongsixinxi";
		try {
			DataResult dr=DBHelper.executeQuery(sql);
			while(dr.rs.next()) {
				int id=dr.rs.getInt("id");
				String rexian=dr.rs.getString("rexian");
				String dianhua=dr.rs.getString("dianhua");
				String youxiang=dr.rs.getString("youxiang");
				String bizhi=dr.rs.getString("bizhi");
				Gongsixinxi cp=new Gongsixinxi(id,rexian,dianhua,youxiang,bizhi);
				list.add(cp);
			}
			dr.closeAll();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return list;
	} 
}
